package com.Iconcat.system.service.impl;

import com.Iconcat.system.entity.Investment;
import com.Iconcat.system.entity.WithdrawalRecord;
import com.Iconcat.system.mapper.InvestmentMapper;
import com.Iconcat.system.response.Result;
import com.Iconcat.system.service.InvestmentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zzq
 * @since 2023-12-19
 */
@Service
public class InvestmentServiceImpl extends ServiceImpl<InvestmentMapper, Investment> implements InvestmentService {

    @Override
    public void recordInvestment(Integer projectId, Integer userId, BigDecimal investmentAmount) throws Exception{
        try {
            Investment investment = new Investment();
            investment.setProjectId(projectId);
            investment.setUserId(userId);
            investment.setInvestmentAmount(investmentAmount);

            investment.setInvestmentStatus("已投资");  // 根据实际情况设置投资状态

            // 调用 MyBatis-Plus 的 save 方法保存投资信息到数据库
            save(investment);

            // 如果保存成功，可以在这里执行其他相关操作

        } catch (Exception e) {
            // 如果发生异常，可以选择抛出自定义异常或者记录日志
            e.printStackTrace();
            throw new Exception("记录投资信息失败");
        }
    }

    @Override
    public List<Investment> getInvestmentsByProjectId(Integer projectId) {
        LambdaQueryWrapper<Investment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Investment::getProjectId, projectId);
        queryWrapper.eq(Investment::getInvestmentStatus, "已投资");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public boolean hasInvested(Integer userId, Integer projectId) {
        Integer result = baseMapper.hasInvested(userId, projectId);
        return result != null && result > 0;
    }

    @Override
    public Result updateStatusToWithdrawn(Integer userId, Integer projectId) {

        UpdateWrapper<Investment> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId)
                .eq("project_id", projectId)
                .set("project_earnings", BigDecimal.ZERO)
                .set("investment_status", "已提现"); // 根据你的数据库字段进行调整

        int updateCount = baseMapper.update(null, updateWrapper);
        if (updateCount == 0) {
            // 表示没有匹配的记录，可能需要处理相应的逻辑
            return Result.error().message("更新失败，没有找到匹配的记录");
        }
        return Result.ok().message("更新成功");
    }

    @Override
    public BigDecimal SelectProjectEarnings(Integer userId, Integer projectId) {

        return  this.baseMapper.SelectProjectEarnings(userId, projectId);
    }

    @Override
    public IPage<Map<String, Object>> selectProjectInvestment(Page<?> page, LambdaQueryWrapper<Investment> queryWrapper) {
        return this.baseMapper.selectProjectInvestment(page,queryWrapper);
    }

    @Override
    public Result updateProjectStatus(Integer projectId, Integer userId) {
        UpdateWrapper<Investment> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId)
                .eq("project_id", projectId)
                .set("investment_status", "已结束"); // 根据你的数据库字段进行调整

        int updateCount = baseMapper.update(null, updateWrapper);
        if (updateCount == 0) {
            // 表示没有匹配的记录，可能需要处理相应的逻辑
            return Result.error().message("更新失败，没有找到匹配的记录");
        }
        return Result.ok().message("更新成功");
    }


}
